package bluewhale.shuku.preprocess;

/**
 * @author peeking
 */

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

public class PageCollection {
	
	public static String urlFeature = "[wbia project/BlueWhale][url]:";
	public static String contentHeadFeature = "[wbia project/BlueWhale][content]:";
	public static String contentTailFeature = "[wbia project/BlueWhale][end of content]";
	
	private BufferedReader source = null;
	private File file = null;
	
	public PageCollection(String file) {
		this(new File(file));
	}
	
	public PageCollection(File file) {
		this.file = file;
	}
	
	public boolean next() throws IOException {
		this.url = null;
		this.content = null;
		StringBuffer buffer = new StringBuffer();
		
		if (source == null)
			source = new BufferedReader(new FileReader(this.file));
		String line = null;
		while ((line = source.readLine()) != null && "".equals(line.trim()))
			; // skip empty line
		if (line == null)
			return false;
		if (line.startsWith(urlFeature))
			this.url = line.substring(urlFeature.length()).trim();
		line = source.readLine();	// skip head feature
		while ((line = source.readLine()) != null) {
			if (line.startsWith(contentTailFeature)) {
				this.content = buffer.toString();
				break;
			}
			buffer.append(line).append("\n");
		}
		
		return true;
	}
	
	public void reset() throws IOException {
		if (source != null) {
			source.close();
			source = new BufferedReader(new FileReader(this.file));
		}
	}
	
	public void close() throws IOException {
		if (source != null)
			source.close();
	}
	
	private String url = null;
	private String content = null;
	
	public String getUrl() {
		return url;
	}

	public String getContent() {
		return content;
	}
	

	public static void main(String[] args) {

	}


}
